<?php
/**
 * Larfree Api类
 * @author xiao
 */
namespace App\Http\Controllers\Api\Express;
use App\Models\User\UserHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Larfree\Controllers\ApisController as Controller;
use App\Models\Express\ExpressNews;
class NewsController extends Controller
{
    public function __construct(ExpressNews $model)
    {
        $this->model = $model;
        parent::__construct();
    }

    public  function    index(Request $request)
    {
        if($request->type){
            $this->model=$this->model->query()->where('type',$request->type);
        }
        if($request->chosed){
            $this->model=$this->model->where('chosed',1);
        }
        $ways=$request->ways;
        $this->model=$this->model->select(['id','banner_url','type','read_number','click_number','brief_info','created_at', 'order','title']);
        if($ways=='read'){
            $this->model=$this->model->orderBy('click_number', 'desc');
        }  elseif ($ways=='latest'){
            $this->model=$this->model->orderBy('created_at', 'desc');
        }else{
            $this->model=$this->model->orderBy('click_number', 'asc');
        }
        return parent::index($request); // TODO: Change the autogenerated stub
    }

    public  function  recommendNews(){
        return $this->model->query()->recomend()->get();
    }

    public  function weekHotNews(){
        return $this->model->query()
            ->where('created_at','>',date("Y-m-d",strtotime("-7 day")))
            ->limit(7)
            ->orderBy('read_number')->get();
    }


    public  function  show($id, Request $request)
    {

             $userHistory=new UserHistory();
             $userHistory->logUserHistory($id); //记录用户浏览历史

            Cache::remember('news'.$id.$request->getClientIp(), 1, function ()  use ($id){
                $news=$this->model->where('id',$id)->first();
                $news->increment('read_number',1);
                $news->save();
                return $news;
            });

        $after = $this->model->select(['id','title'])->where('id','>',$id)->orderBy('id','asc')->first();  //上一页新闻
        $before  =$this->model->select(['id','title'])->where('id','<',$id)->orderBy('id','desc')->first(); //下一页新闻
        $data=[
             'content'=>$this->model->find($id),
             'before'=>$before,
             'after'=>$after,
        ];
        return $data;

    }

    public  function addClick(Request $request){
        $id=$request->id;
        $news=$this->model->select(['click_number','id'])->where('id',$id)->first();
        $news->increment('click_number',1);
        $news->save();
        return $news;
  /*     $news= Cache::remember('news_click'.$id.$request->getClientIp(), 1, function ()  use ($id){
            $news=$this->model->select(['click_number','id'])->where('id',$id)->first();
            $news->increment('click_number',1);
            $news->save();
            return $news;
        });
        return $news;*/
    }
}